package com.example.authservice.service.impl;

import com.example.authservice.entity.JwtUser;
import com.example.authservice.pojo.po.DeptPO;
import com.example.authservice.pojo.po.UserPO;
import com.example.authservice.service.DeptService;
import com.example.authservice.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * @author 鱼凫
 * @version 1.0
 * Create by 2023/4/24 22:37
 * @Description 用于security获取用户对象
 */
@Service
@Component("UserDetailsServiceImpl")
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserService userService;

    @Autowired
    private DeptService deptService;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        UserPO user = userService.findByUsername(s);
        DeptPO dept = deptService.selectOneById(user.getDept());
        return new JwtUser(user,dept.getRole());
    }

}
